package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.activitity.fourmodule.ApplyDetailActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.javaBean.StreetInfoListBean;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;


public class ShouliCenterDetailActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private ImageView regist_back;
    private TextView tv_headtitle;
    private int state;
    private ImageView iv_head;
    private RelativeLayout layout_shenhe;
    private TextView tv_name;
    private TextView tv_time;
    private TextView tv_cancelshenhe;
    private String title;
    private long time;
    private long finishTime;
    private String id;
    private TextView tv_delete;
    private RelativeLayout layout_shenhefail;
    private RelativeLayout layout_pingjia;
    private TextView tv_pingjiasubmit;
    private RelativeLayout layout_shenhefinish;
    private TextView tv_timefinish;
    private TextView tv_timesbumit;
    private TextView tv_namefinish;
    private int getType;
    private int getSubType;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private final String DELETEHOUSEDELETEFLAG="DELETEHOUSEDELETEFLAG";
    private final String UPDATESERVERBYIDFLAG="UPDATESERVERBYIDFLAG";
    private MyToast myToast;
    private EditText et_communt;
    private RatingBar ratingBar;
    private TextView tv_descmark;
    private TextView tv_cancelshenhefail;
    private String remarks;
    private String againId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoulicenterdetail_layout);
        initView();
        Intent intent=getIntent();
        context=this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
        state=intent.getIntExtra("state",0);
        title=intent.getStringExtra("title");
        time=intent.getLongExtra("time",0);
        finishTime=intent.getLongExtra("finishTime",0);
        id=intent.getStringExtra("id");
        getType=intent.getIntExtra("type",0);
        getSubType=intent.getIntExtra("subType",0);
        remarks=intent.getStringExtra("remarks");
        againId=intent.getStringExtra("againId");
        if (state == 0) {
            iv_head.setVisibility(View.VISIBLE);
            layout_shenhe.setVisibility(View.VISIBLE);
            //审核中
            iv_head.setImageDrawable(getResources().getDrawable(R.mipmap.grouptijiao));
            tv_name.setText(title);
            tv_time.setText(Tools.timedateyyyy_MM_dd(String.valueOf(time / 1000)));
        } else if (state == 1) {
            iv_head.setVisibility(View.VISIBLE);
            //待评价
            iv_head.setImageDrawable(getResources().getDrawable(R.mipmap.grouppingjia));
            layout_pingjia.setVisibility(View.VISIBLE);
            tv_pingjiasubmit.setVisibility(View.VISIBLE);
        }else if (state == 2) {
            //完成
            layout_shenhefinish.setVisibility(View.VISIBLE);
            tv_namefinish.setText(title);
            tv_timesbumit.setText(Tools.timedateyyyy_MM_dd(String.valueOf(time / 1000)));
            if(finishTime!=0)
            {
                tv_timefinish.setText(Tools.timedateyyyy_MM_dd(String.valueOf(finishTime / 1000)));
            }
        }  else if (state == 3) {
            //审核失败
            tv_delete.setVisibility(View.VISIBLE);
            layout_shenhefail.setVisibility(View.VISIBLE);
        }
    }

    private void initView()
    {
        et_communt=(EditText) findViewById(R.id.et_communt);
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("业务详情");
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        iv_head=(ImageView) findViewById(R.id.iv_head);
        layout_shenhe=(RelativeLayout) findViewById(R.id.layout_shenhe);
        tv_name=(TextView) findViewById(R.id.tv_name);
        tv_time=(TextView) findViewById(R.id.tv_time);
        tv_cancelshenhe=(TextView) findViewById(R.id.tv_cancelshenhe);
        tv_cancelshenhe.setOnClickListener(this);
        tv_delete=(TextView) findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);
        layout_shenhefail=(RelativeLayout) findViewById(R.id.layout_shenhefail);
        layout_pingjia=(RelativeLayout) findViewById(R.id.layout_pingjia);
        tv_pingjiasubmit=(TextView) findViewById(R.id.tv_pingjiasubmit);
        tv_pingjiasubmit.setOnClickListener(this);
        layout_shenhefinish=(RelativeLayout) findViewById(R.id.layout_shenhefinish);

        tv_timefinish=(TextView) findViewById(R.id.tv_timefinish);
        tv_timesbumit=(TextView) findViewById(R.id.tv_timesbumit);
        tv_namefinish=(TextView) findViewById(R.id.tv_namefinish);
        ratingBar=(RatingBar) findViewById(R.id.ratingBar);

        tv_descmark=(TextView) findViewById(R.id.tv_descmark);
        tv_cancelshenhefail=(TextView) findViewById(R.id.tv_cancelshenhefail);
        tv_descmark.setOnClickListener(this);
        tv_cancelshenhefail.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            //审核中的删除按钮
            case R.id.tv_cancelshenhe:
                contributorPresenter.getCommunityDelServerById(id,getType,getSubType,DELETEHOUSEDELETEFLAG);
                break;
            //审核失败里的删除按钮
            case R.id.tv_delete:
                contributorPresenter.getCommunityDelServerById(id,getType,getSubType,DELETEHOUSEDELETEFLAG);
                break;
            //审核失败里的审核失败原因
            case R.id.tv_descmark:
                if(remarks!=null)
                {
                    iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                    idb.setTitle("审核失败原因");
                    idb.setMessage(remarks);
                    idb.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    idb.show();
                }
                break;
            //审核失败里的重新提交按钮
            case R.id.tv_cancelshenhefail:
                String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
                String cidNumber="";
                if (locationString.length() > 0) {
                    StreetInfoListBean streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
                    cidNumber=streetInfoListBean.getId();
                }
                Intent intent = new Intent(context, ApplyDetailActivity.class);
                intent.putExtra("id", againId);
                intent.putExtra("cid",cidNumber);
                intent.putExtra("type",getType);
                intent.putExtra("name", title);
                intent.putExtra("source",ShouliCenterDetailActivity.class.getSimpleName());
                intent.putExtra("previousId",id);
                startActivityForResult(intent,0);
                break;
            //评分提交
            case R.id.tv_pingjiasubmit:
                String comment=et_communt.getText().toString().trim();
                int score=(int) ratingBar.getRating();
                if(score>0)
                {
                    contributorPresenter.getCommunityUpdateServerById(id,getType,getSubType,score,comment,UPDATESERVERBYIDFLAG);
                }else
                {
                    myToast.showShort("评分不能为空");
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0&& resultCode==1)
        {
            finish();
        }

    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(DELETEHOUSEDELETEFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("删除成功");
                        finish();
                    }else {
                        myToast.showShort("删除失败");
                    }
                }else
                {
                    myToast.showShort("删除失败");
                }
            }else if(flag.equals(UPDATESERVERBYIDFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("评价成功");
                        finish();
                    }else {
                        myToast.showShort("评价失败");
                    }
                }else
                {
                    myToast.showShort("评价失败");
                }
            }

        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }
}
